// pages/member-inviteNeighbor/member-inviteNeighbor.js
import api from '../../utils/api.js'
let app = getApp();
import {
    HTTP
} from "../../utils/http.js";
import { config } from '../../utils/config'
let http = new HTTP();
Page({
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        showModel: false, // 模态窗口
        showDetail: false, //展示 规则
        authorizeBtn: false,
        imgUrl: config.imgUrl,
        shuffled: ''
    },
    onLoad: function(options) {
        let user_id = wx.getStorageSync('user_id');
        let group = wx.getStorageSync('inviteOrganizationInfo');
        if (group) {
            this.setData({
                group
            })
        }

        this.getNeighborList(user_id);
    },

    /**邀请用户入团，，，暂定 只能团长去邀请
     * id————邀请人ID
     * rid————被邀请用户ID
     * tname————社团名
     * uname————被邀请用户名字
     * yname————邀请人名字
     * organization_id————团id
     * apply_or_invite: 1——申请  2——邀请
     */
    inviteUserJoinOrganization(e) {
        let self = this;
        let user_id = wx.getStorageSync('user_id');
        let tname = this.data.group.name;
        let yname = this.data.group.user_name;
        let organization_id = this.data.group.id;

        let rid = e.currentTarget.dataset.rid;
        let uname = e.currentTarget.dataset.uname;
        http.request({
            url: api.gotoOrganization,
            data: {
                id: user_id,
                rid,
                tname,
                yname,
                uname,
                organization_id,
                apply_or_invite: 2
            },
            success: (res) => {
                console.log('邀请用户入团', res);
                if (res.code == 200) {
                    wx.showToast({
                        title: '邀请成功',
                        icon: 'success',
                        duration: 1500,
                        mask: true,
                        success: () => {
                            setTimeout(() => {
                                self.onLoad()
                            }, 2000)
                        }
                    })
                }
            }
        });
    },
    //拿到发送者的状态  huo 邀请者的 状态
    /**
     * send_id——发送者ID
     * organization_id——发送团ID
     * user_id——发送对象ID
     */
    getSendStatus(user_id, ele, arr) {
        http.request({
            url: api.getSendStatus,
            data: {
                send_id: wx.getStorageSync('user_id'),
                organization_id: this.data.group.id,
                user_id
            },
            success: (res) => {
                console.log('用户发送邀请或者申请的状态', res);
                if (res.code == 200) {
                    let type = res.data[0].type
                    if (type == 1) {
                        ele.stautsIng = true
                    }
                    this.setData({
                        neighborList: arr,
                        shuffled: arr
                    })
                }
            }
        });
    },
    //拿到用户附近的人
    getNeighborList(user_id) {
        http.request({
            url: api.getNeighborList,
            data: {
                id: user_id
            },
            success: (res) => {
                console.log('拿到用户附近的人', res);
                if (res.code == 200) {
                    this.setData({
                        shuffled: res.data
                    })
                    res.data.map((ele, index, arr) => {
                        return this.getSendStatus(ele.id, ele, arr)
                    })
                }
            }
        });
    },
    //社区团————换一批,洗牌
    changeBatch(e, arr = this.data.neighborList, count = 0) {
        // if (arr.length){
        //   return false;
        // }

        let shuffled = arr.slice(0),
            i = arr.length,
            min = i - count,
            temp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            temp = shuffled[index];
            shuffled[index] = shuffled[i];
            shuffled[i] = temp;
        }
        shuffled = shuffled.slice(min);
        console.log('shuffled', shuffled)
        this.setData({
            shuffled
        })
        return shuffled
    },
    // 展示规则
    showDetail(e) {
        let showDetail = this.data.showDetail;
        let showModel = this.data.showModel;

        this.setData({
            showDetail: !showDetail,
            showModel: !showModel
        })

    },
    close(e) {
        this.setData({
            showDetail: false,
            showModel: false
        })
    },

    onUnload: function() {

    },

    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

})